import com.mit.web.business.proxy.MaintenanceProxy;
import com.mit.web.business.remote.IInvokeResponder;
import com.mit.web.config.Constants;
import com.mit.web.config.SizeGroupConfig;
import com.mit.web.event.PopWinCloseEvent;
import com.mit.web.util.FlexUtil;
import com.mit.web.util.LogUtil;
import com.mit.web.util.MessageUtil;
import com.mit.web.util.Validator;
import com.mit.web.util.FlexSession;
import com.mit.web.view.component.monitor.IProgressMonitor;
import com.mit.web.vo.maintenance.EmployeeVO;

import flash.events.MouseEvent;
import flash.geom.Point;

import mx.controls.Alert;
import mx.controls.DataGrid;
import mx.core.UIComponent;
import mx.events.FlexEvent;
import mx.logging.ILogger;
import mx.resources.ResourceManager;
import mx.rpc.events.ResultEvent;
import mx.utils.StringUtil;

private var _logger:ILogger;
[Bindable]private var _empVo:EmployeeVO = new EmployeeVO();
private var _proxy:MaintenanceProxy = MaintenanceProxy.getInstance();
private var _pm:IProgressMonitor;


private function init():void{
	_logger = LogUtil.getLogger(this);
	_logger.debug("initialize preference windows");
	
	copyProperties(FlexSession.currentUser.employee);
}

private function doSave(event:MouseEvent):void{
	if(mx.validators.Validator.validateAll(validators).length == 0 ){
		// validate data
		if(!validate()) {
			return;
		}
		
		_empVo.username = txt_username.text;
		if (!com.mit.web.util.Validator.isNullOrBlank(txt_newpwd.text)) {
			_empVo.password = txt_newpwd.text;
		}
		_empVo.email = txt_email.text;
		_empVo.mobile = txt_mobile.text;
		_logger.debug("_empVo.username=" + _empVo.username + ", _empVo.password=" + _empVo.password 
			+ ", _empVo.email=" + _empVo.email + ", _empVo.mobile=" + _empVo.mobile);
		
		var responder:IInvokeResponder;
		responder = _proxy.updateEmployee(_empVo).installProgressBar(_pm, this);
		responder.addFaultListener(exceptionHandler);
		responder.addResultListener(savedHandler);
	} else {
		event.preventDefault();
	}
}

private function copyProperties(employeeVo:EmployeeVO):void {
	_empVo.id = employeeVo.id;
	_empVo.name = employeeVo.name;
	_empVo.mobile = employeeVo.mobile;
	_empVo.email = employeeVo.email;
	_empVo.username = employeeVo.username;
	_empVo.activeInd = employeeVo.activeInd;
	_empVo.lastUpdatedTime = employeeVo.lastUpdatedTime;
	_empVo.password = employeeVo.password;
	_empVo.adminInd = employeeVo.adminInd;
	_empVo.sex = employeeVo.sex;
}

private function savedHandler(event:ResultEvent):void{
	var empVo:EmployeeVO = event.result as EmployeeVO;	
	copyProperties(empVo);
	
	FlexSession.currentUser.username = empVo.username;
	FlexSession.currentUser.password = empVo.password;
	FlexSession.currentUser.employee.email = empVo.email;
	FlexSession.currentUser.employee.mobile = empVo.mobile;
	FlexSession.currentUser.employee = empVo;
	
//	closeWindow(this);
	var message:String = ResourceManager.getInstance().getString(Constants.BUNDLE_COMMON,"info.common.saved",["个人设定"]);
	showMessageTip(message, this);
}

private function validate():Boolean{
	var rtn:Boolean = true;
	if(rtn && com.mit.web.util.Validator.isNullOrBlank(txt_mobile.text)){
		showRequiredErrorTip(txt_mobile);
		rtn = false;
	}
//	if(rtn && com.mit.web.util.Validator.isNullOrBlank(txt_email.text)){
//		showRequiredErrorTip(txt_email);
//		rtn = false;
//	}
	if(rtn && com.mit.web.util.Validator.isNullOrBlank(txt_username.text)){
		showRequiredErrorTip(txt_username);
		rtn = false;
	}
	
	// check if password not matched
	if (rtn) {
		// check old password
		_logger.debug("_empVo.password=" + _empVo.password + ", txt_pwd.text="
			+ txt_pwd.text);
		if (_empVo.password != txt_pwd.text) {
			showErrorTip("请正确输入旧密码", txt_pwd);
			rtn = false;
		}
	}
	return rtn;
}